package cn.yo2.irain.common.util;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

public class CMyException extends Exception {

    protected int errNo;
    protected Throwable rootCause;

    public CMyException(int _errNo) {
        errNo = 0;
        rootCause = null;
        errNo = _errNo;
    }

    public CMyException(int _errNo, String _sMsg) {
        super(_sMsg);
        errNo = 0;
        rootCause = null;
        errNo = _errNo;
    }

    public CMyException(String _sMsg) {
        super(_sMsg);
        errNo = 0;
        rootCause = null;
    }

    public CMyException(int _errNo, String _sMsg, Throwable _rootCause) {
        super(_sMsg);
        errNo = 0;
        rootCause = null;
        errNo = _errNo;
        rootCause = _rootCause;
    }

    public CMyException(String _sMsg, Throwable _rootCause) {
        super(_sMsg);
        errNo = 0;
        rootCause = null;
        rootCause = _rootCause;
    }

    public int getErrNo() {
        return errNo;
    }

    public Throwable getRootCause() {
        return rootCause;
    }

    public String getMyMessage() {
        return super.getMessage();
    }

    public String toString() {
        String sMessage = "[ERR-" + errNo + "] " + getMyMessage();
        return sMessage;
    }

    public String getMessage() {
        String sMessage = toString();
        if (rootCause != null) {
            sMessage = sMessage + "\r\n<-- " + rootCause.toString();
        }
        return sMessage;
    }

    public String getLocalizedMessage() {
        return getMessage();
    }

    public void printStackTrace(PrintStream _ps) {
        if (rootCause == null) {
            super.printStackTrace(_ps);
        } else {
            Throwable root = rootCause;
            synchronized (_ps) {
                _ps.println(toString());
                Throwable temp = null;
                while (root instanceof CMyException) {
                    _ps.println("<-- " + root.toString());
                    temp = root;
                    root = ((CMyException) root).getRootCause();
                    if (root == null) {
                        temp.printStackTrace(_ps);
                        break;
                    }
                }
                if (root != null) {
                    _ps.print("<-- ");
                    root.printStackTrace(_ps);
                }
            }
        }
    }

    public void printStackTrace(PrintWriter _pw) {
        if (rootCause == null) {
            super.printStackTrace(_pw);
        } else {
            Throwable root = rootCause;
            synchronized (_pw) {
                _pw.println(toString());
                Throwable preRoot = null;
                for (; root instanceof CMyException; _pw.println(preRoot.toString())) {
                    _pw.print("<-- ");
                    preRoot = root;
                    root = ((CMyException) root).getRootCause();
                    if (root != null) {
                        continue;
                    }
                    preRoot.printStackTrace(_pw);
                    break;
                }

                if (root != null) {
                    _pw.print("<-- ");
                    root.printStackTrace(_pw);
                }
            }
        }
    }

    public String getStackTraceText() {
        return getStackTraceText(((Throwable) (this)));
    }

    public static String getStackTraceText(Throwable _ex) {
        StringWriter strWriter;
        PrintWriter prtWriter;
        strWriter = null;
        prtWriter = null;
        String s;
        try {
            strWriter = new StringWriter();
            prtWriter = new PrintWriter(strWriter);
            _ex.printStackTrace(prtWriter);
            prtWriter.flush();
            s = strWriter.toString();
        }
        catch (Exception ex) {
            s = _ex.getMessage();
            return s;
        }
        finally {
            if (strWriter != null) {
                try {
                    strWriter.close();
                }
                catch (Exception exception1) {
                }
            }
            if (prtWriter != null) {
                try {
                    prtWriter.close();
                }
                catch (Exception ex) {
                }
            }
        }
        return s;
    }

    public static final void main(String args[]) {
        CMyException fire0 = new CMyException(1, "my exception 0");
        CMyException fire = new CMyException(1, "my exception 1", fire0);
        CMyException fire2 = new CMyException(10, "my exception 2", fire);
        fire2.printStackTrace(System.out);
        System.out.println("-------------------");
        System.out.println(fire2.getMessage());
        System.out.println("-------------------");
        System.out.println(fire2.getStackTraceText());
        try {
            int a = 0;
            int b = 1 / a;
            System.out.println(b);
        }
        catch (Exception ex) {
            System.out.println(getStackTraceText(ex));
        }
    }
}
